package views;

import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.*;

public class Console extends JFrame {
	
	public static final String DATE_FORMAT_NOW = "H:mm:ss";

	private static final int CONSOLE_CHARS = 10000;
	private static final int CONSOLE_ROWS = 10;
	private JTextArea textArea;
	private JScrollPane scrollPane;
	
	/**
	 * Creates a console for posting various information in.
	 * @param name A String describing the console information.
	 */
	public Console(String name) {
		
		this.setSize(400, 220);
		this.setTitle(name);
		this.setLayout(null);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setLocation(500, 500);
		textArea = new JTextArea();
		textArea.setRows(CONSOLE_ROWS);
		textArea.setEditable(false);
		textArea.setFont(new Font("Verdana",Font.PLAIN,9));
		scrollPane = new JScrollPane(textArea);
		scrollPane.setBounds(0, 0, 395, 195);
		this.add(scrollPane);
		
		this.setResizable(false);
		this.setVisible(true);
		
		addString("Console loaded!");
	}
	
	public synchronized void addString(String string) {
		textArea.setText(textArea.getText() + now() + " >> " + string + "\n");
		String text = textArea.getText();
		if(text.length() > CONSOLE_CHARS) {
			textArea.setText(text.substring(text.length()-CONSOLE_CHARS, text.length()));
		}
	}
	
	private static String now() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		return sdf.format(cal.getTime());
	}

}